package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.JwClazzInfoBo;
import org.dromara.system.domain.vo.JwClazzInfoVo;
import org.dromara.system.domain.JwClazzInfo;
import org.dromara.system.mapper.JwClazzInfoMapper;
import org.dromara.system.service.IJwClazzInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 班级Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-26
 */
@RequiredArgsConstructor
@Service
public class JwClazzInfoServiceImpl implements IJwClazzInfoService {

    private final JwClazzInfoMapper baseMapper;

    /**
     * 查询班级
     */
    @Override
    public JwClazzInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询班级列表
     */
    @Override
    public TableDataInfo<JwClazzInfoVo> queryPageList(JwClazzInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JwClazzInfo> lqw = buildQueryWrapper(bo);
        Page<JwClazzInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询班级列表
     */
    @Override
    public List<JwClazzInfoVo> queryList(JwClazzInfoBo bo) {
        LambdaQueryWrapper<JwClazzInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JwClazzInfo> buildQueryWrapper(JwClazzInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JwClazzInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClazzId()), JwClazzInfo::getClazzId, bo.getClazzId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), JwClazzInfo::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getMajorId()), JwClazzInfo::getMajorId, bo.getMajorId());
        return lqw;
    }

    /**
     * 新增班级
     */
    @Override
    public Boolean insertByBo(JwClazzInfoBo bo) {
        JwClazzInfo add = MapstructUtils.convert(bo, JwClazzInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改班级
     */
    @Override
    public Boolean updateByBo(JwClazzInfoBo bo) {
        JwClazzInfo update = MapstructUtils.convert(bo, JwClazzInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JwClazzInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除班级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
